/** Description of SmokeTimerTask.java 
 *
 * @author panther
 * Jayant Pratim Saikia
 * jay.saikia@gmail.com
 *
 */
package com.panther.awp.android.smoker.async;

import java.util.Calendar;
import java.util.Date;

import android.appwidget.AppWidgetManager;
import android.content.ComponentName;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.RemoteViews;

import com.panther.awp.android.smoker.R;
import com.panther.awp.android.smoker.widgets.SmallWidgetProvider;

public class SmokeTimerTask extends AsyncTask<Void, Void, Void> {
	
	Context context;
	int timeToWait;
	
	public SmokeTimerTask(Context  context) {
		this.context = context;
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		try {
			timeToWait = Integer.parseInt(prefs.getString("avgTime", "5"));
		} catch (NumberFormatException e) {
			timeToWait = 5;
		}
	}
	
	@Override
	protected void onPreExecute() {
		super.onPreExecute();
		
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.smallsmokewidgetlayout);
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		ComponentName thisWidget = new ComponentName(context, SmallWidgetProvider.class);
		
		//remoteViews.setInt(R.id.smallWidgetBrandScrollDown, "setBackgroundResource", R.drawable.small_widget_cigarette_burn);
		remoteViews.setViewVisibility(R.id.smallWidgetBrandScrollDown, View.GONE);
		remoteViews.setViewVisibility(R.id.smallWidgetBrandScrollDown_burn, View.VISIBLE);
		remoteViews.setViewVisibility(R.id.smallWidgetSmokeButton, View.GONE);
		remoteViews.setViewVisibility(R.id.smallWidgetSmokeButton_disabled, View.VISIBLE);
		
		appWidgetManager.updateAppWidget(thisWidget, remoteViews);
		
	}

	@Override
	protected Void doInBackground(Void... params) {
		
		//######### Updating Preferences ###############
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
		SharedPreferences.Editor editor1 = prefs.edit();
		editor1.putBoolean("sStatus", true);
		editor1.commit();
		//######### Updating Preferences Complete ######
		
		Date currentTime = new Date();
		Date endTime = new Date();
		endTime.setTime(endTime.getTime() + timeToWait*60*1000L);
		while (currentTime.before(endTime) && prefs.getBoolean("sStatus", false)) {
			currentTime.setTime(Calendar.getInstance().getTimeInMillis());
		}
		
		//######### Updating Preferences ###############
		SharedPreferences.Editor editor2 = prefs.edit();
		editor2.putBoolean("sStatus", false);
		editor2.commit();
		//######### Updating Preferences Complete ######
		if(isCancelled()) {
			//######### Updating Preferences ###############
			SharedPreferences.Editor editor3 = prefs.edit();
			editor3.putBoolean("sStatus", false);
			editor3.commit();
			//######### Updating Preferences Complete ######
		}
		return null;
	}

	@Override
	protected void onPostExecute(Void result) {
		super.onPostExecute(result);
		
		RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.smallsmokewidgetlayout);
		AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
		ComponentName thisWidget = new ComponentName(context, SmallWidgetProvider.class);
		
		//remoteViews.setInt(R.id.smallWidgetBrandScrollDown, "setBackgroundResource", R.drawable.small_widget_cigarette_noburn);
		remoteViews.setViewVisibility(R.id.smallWidgetBrandScrollDown, View.VISIBLE);
		remoteViews.setViewVisibility(R.id.smallWidgetBrandScrollDown_burn, View.GONE);
		remoteViews.setViewVisibility(R.id.smallWidgetSmokeButton, View.VISIBLE);
		remoteViews.setViewVisibility(R.id.smallWidgetSmokeButton_disabled, View.GONE);
		
		appWidgetManager.updateAppWidget(thisWidget, remoteViews);
	}

	
}
